﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading;
using Conusic.Properties;

namespace Conusic
{
    public struct Group
    {
        public Group(string id, string name, string avataruri)
            : this()
        {
            Id = id;
            Name = name;
            AvatarUri = avataruri;
        }

        public string Id { get; private set; }
        public string Name { get; private set; }
        public string AvatarUri { get; private set; }

        public override string ToString()
        {
            return this.Name;
        }

        public static explicit operator Profile(Group group)
        {
            var temp = new Profile(ProfileTypes.Group, group.Id, group.Name);
            return temp;
        }
    }

    /// <summary>
    /// Description of VkontakteGroups.
    /// </summary>
    public static class VkontakteGroups
    {
        //
        private const string RegexGroup =
            @"<img src=""([^""]*)"".{0,512}?href=""[^""]*"">([^<]*)<.{0,256}?c\[group\]=([0-9]*)";

        private static List<Group> ParseGroups(string page)
        {
            var regexpr = new Regex(RegexGroup, RegexOptions.Singleline);
            Match match = regexpr.Match(page);
            var result = new List<Group>();
            while (match.Success)
            {
                string id = match.Groups[3].ToString();
                string name = match.Groups[2].ToString();
                string avatarURI = match.Groups[1].ToString();
                avatarURI = avatarURI.Replace("\\", string.Empty);

                if (!avatarURI.Contains("http://"))
                    avatarURI = avatarURI.Insert(0, Settings.Default.VkontakteAddress);

                match = match.NextMatch();

                if(String.IsNullOrEmpty(id))
                    continue;

                result.Add(new Group(id, name, avatarURI));
            }
            return result;
        }

        public static void UpdateGroupsAsync(IGroupsListCommand command)
        {
            ThreadStart threadStart = delegate() { UpdateGroups(command); };
            var thread = new Thread(threadStart);
            thread.Start();
        }

        public static List<Group> UpdateGroups()
        {
            return UpdateGroups(null);
        }

        public static List<Group> UpdateGroups(IGroupsListCommand command)
        {
            try
            {
                string page = SimpleHttp.RequestVkontaktePage("groups.php");

                var result = ParseGroups(page);

                if (command != null)
                    command.Execute(result);

                return result;
            }
            catch (Exception e)
            {
                if (command != null)
                {
                    command.Fail(e);
                    return null;
                }
                throw;
            }
        }
    }
}